package com.jack.thinking.java._22.swing.concurrent;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;

import javax.swing.*;

import com.utils.Bad;
import com.utils.SwingConsole;

@Bad
public class LongRunningTask extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton b1 = new JButton("Start Long Running Task");
	private JButton b2 = new JButton("End Long Running Task");
	
	public LongRunningTask() {
		b1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					TimeUnit.SECONDS.sleep(3);
				} catch (InterruptedException e1) {
					System.out.println("Task interrupted");
					return;
				}
				System.out.println("Task completed");
			}
			
		});
		
		b2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Thread.currentThread().interrupt();
			}
		});
		
		setLayout(new FlowLayout());
		add(b1);
		add(b2);
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new LongRunningTask(), 200, 150);
	}
}
